﻿using System;

public partial class aspx_admin_video_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strid = Request.QueryString["id"];
            int id = 0;
            if (string.IsNullOrEmpty(strid) || !int.TryParse(strid, out id))
            {
                Response.Redirect("view.aspx");
            }
            this.hdf视频ID.Value = strid;
            try
            {
                ds_名师讲堂.v_名师讲堂视频Row dr = null;
                using (ds_名师讲堂TableAdapters.v_名师讲堂视频TableAdapter da = new ds_名师讲堂TableAdapters.v_名师讲堂视频TableAdapter())
                {
                    dr = da.GetDataBy视频ID(id).Rows[0] as ds_名师讲堂.v_名师讲堂视频Row;
                }
                if (dr != null)
                {
                    this.txt发布时间.Text = dr.发布时间.ToString();
                    this.txt点击量.Text = dr.点击量.ToString();
                    this.ddl讲堂ID.SelectedValue = dr.讲堂ID.ToString();
                    this.ccb置顶.Checked = dr.置顶;
                    this.mv视频类别.ActiveViewIndex = 0;
                    return;
                }
            }
            catch (Exception)
            {
                ds_名师讲堂.v_其他类别视频Row drr = null;
                using (ds_名师讲堂TableAdapters.v_其他类别视频TableAdapter da = new ds_名师讲堂TableAdapters.v_其他类别视频TableAdapter())
                {
                    drr = da.GetDataBy视频ID(id).Rows[0] as ds_名师讲堂.v_其他类别视频Row;
                }
                if (drr != null)
                {
                    this.txt发布时间.Text = drr.发布时间.ToString();
                    this.txt点击量.Text = drr.点击量.ToString();
                    this.txt标题.Text = drr.标题;
                    this.txt详情.Text = drr.详情;
                    this.ccb置顶.Checked = drr.置顶;
                    this.mv视频类别.ActiveViewIndex = 1;
                    return;
                }
            }

            Response.Redirect("view.aspx");
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("view.aspx");
    }

    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Now;
        int dianji = 0;
        if (!DateTime.TryParse(txt发布时间.Text, out dt))
        {
            WebHelper.MessageBox.Show("请输入正确的时间。");
            return;
        }
        if (!int.TryParse(txt点击量.Text, out dianji))
        {
            WebHelper.MessageBox.Show("输入的点击量不正确。");
            return;
        }

        string filename = string.Empty;

        if (fu.HasFile)
        {
            filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(fu.FileName);
            try
            {
                WebHelper.WebControls.UploadFile(
                         fu,
                         "~/video/",
                         new string[] { ".flv" },
                         filename,
                         false,
                         int.MaxValue);
            }
            catch (Exception)
            {
                throw;
            }
            using (ds_名师讲堂TableAdapters.tb_视频TableAdapter da = new ds_名师讲堂TableAdapters.tb_视频TableAdapter())
            {
                WebHelper.FileAndDirectory.DeleteFile(string.Format("~/video/{0}", (da.GetDataBy视频ID(int.Parse(hdf视频ID.Value)).Rows[0] as ds_名师讲堂.tb_视频Row).虚目录));
            }
        }
        else
        {
            using (ds_名师讲堂TableAdapters.tb_视频TableAdapter da = new ds_名师讲堂TableAdapters.tb_视频TableAdapter())
            {
                filename = (da.GetDataBy视频ID(int.Parse(hdf视频ID.Value)).Rows[0] as ds_名师讲堂.tb_视频Row).虚目录;
            }
        }
       
        switch (mv视频类别.ActiveViewIndex)
        {
            case 0:
                string title = string.Empty;

                using (ds_名师讲堂TableAdapters.tb_视频TableAdapter da = new ds_名师讲堂TableAdapters.tb_视频TableAdapter())
                {
                    da.Update(ddl讲堂ID.SelectedItem.Text, "", filename, dianji, dt, ccb置顶.Checked, int.Parse(hdf视频ID.Value));
                }

                break;

            case 1:
                using (ds_名师讲堂TableAdapters.tb_视频TableAdapter da = new ds_名师讲堂TableAdapters.tb_视频TableAdapter())
                {
                    da.Update(txt标题.Text, txt详情.Text, filename, dianji, dt, ccb置顶.Checked, int.Parse(hdf视频ID.Value));
                }
                break;

            default:
                break;
        }
        WebHelper.MessageBox.Show("更新成功！", "view.aspx");
    }
}